@extends('admin.main')
@section('content')
    <div class="daohang">
        <ul>
            <li>
                <button type="button" class="am-btn am-btn-default am-radius am-btn-xs"> 优惠券管理</button>
            </li>
        </ul>
    </div>
    <div class="admin-biaogelist">

        <div class="listbiaoti am-cf">
            <ul class="am-icon-flag on"> 优惠券列表</ul>
        </div>
        <div class="am-btn-toolbars am-btn-toolbar am-kg am-cf">
            <ul>
                <li><input type="text" style="width: 250px;" class="am-form-field am-input-sm am-input-xm" id="code" placeholder="输入code码核销" /></li>
                <li><button type="button" onclick="Write_off()" class="am-btn am-radius am-btn-xs am-btn-success" style="margin-top: -1px;">核销</button></li>
            </ul>
        </div>
        <div class="am-tabs am-margin" data-am-tabs>
            <ul class="am-tabs-nav am-nav am-nav-tabs">
                <li @if(isset($_GET['state']) && $_GET['state'] == 1)class="am-active" @endif onclick="javascript:window.location.href='{{urlAdmin('favorable','getWriteOffList')}}?state=1'"><a href="">核销</a></li>
                <li @if(isset($_GET['state']) && $_GET['state'] == 2)class="am-active" @endif onclick="javascript:window.location.href='{{urlAdmin('favorable','getFavoraableUsed')}}?state=2'"><a href="">已核销</a></li>
            </ul>
            <div class="am-tabs-bd">
                <form class="am-form am-g">
                    <table width="100%" class="am-table am-table-bordered am-table-radius am-table-striped">
                        <thead>
                        <tr class="am-success">
                            <th class="table-id">ID</th>
                            <th class="table-title">优惠券名称</th>
                            <th class="table-type">类型</th>
                            <th class="table-author">折扣/券额</th>
                            <th class="table-author">有效期</th>
                            <th width="163px" class="table-set">操作</th>
                        </tr>
                        </thead>
                        <tbody id="table">
                        @if(!empty($res['data']))
                            @foreach($res['data'] as $v)
                                <tr>
                                    <td>{{$v['id']}}</td>
                                    <td>{{$v['title']}}</td>
                                    <td>@if($v['type']==1)折扣券@endif @if($v['type']==2)代金券@endif @if($v['type']==3)体验券@endif</td>
                                    <td>@if($v['discount']){{$v['discount']}}@else{{$v['cash']}}@endif</td>
                                    <td class="am-hide-sm-only">{{$v['created_at']}}</td>
                                    <td>@if(isset($_GET['state'])&&$_GET['state']==1)<a href="javascript:void(0)" onclick="writeOff({{$v['id']}})" class="abtn">核销</a>@endif<a href="javascript:void(0)" onclick="del(this,{{$v['id']}})" class="abtn ff6600">删除</a></td>
                                </tr>
                            @endforeach
                        @else
                            <tr>
                                <td colspan="8" style="text-align: center"><span>暂无数据~</span></td>
                            </tr>
                        @endif
                        </tbody>
                    </table>
                    @if(!empty($res['pageNoList']))
                    <ul class="am-pagination am-fr">
                        @if(isset($res['prev_page_url']))
                            <li><a href="$res['prev_page_url']">«</a></li>
                        @endif
                        @foreach($res['pageNoList'] as $v)
                            @if($res['current_page']==$v)
                                <li class="am-active"><a href="{{urlAdmin('favorable','getWriteOffList')}}?page={{$v}}@if(isset($_GET['type']))&type={{$_GET['type']}}@endif">{{$v}}</a></li>
                            @else
                                <li><a href="{{urlAdmin('favorable','getWriteOffList')}}?page={{$v}}@if(isset($_GET['type']))&type={{$_GET['type']}}@endif">{{$v}}</a></li>
                            @endif
                        @endforeach
                            @if(isset($res['next_page_url']))
                                <li><a href="$res['next_page_url']">»</a></li>
                            @endif
                    </ul>
                    @endif
                    @include('admin.footer')
                    @section('css')
                        <link type="text/css" src="/assets/layer/skin/default/layer.css" />
                    @endsection
                    @section('js')
                        <script type="text/javascript" src="/assets/layer/layer.js"></script>
                    @endsection
                    @section('script')
                        <script>
                            var state = "{{isset($_GET['state'])?$_GET['state']:0}}";
                            function del(_this,id){
                                if(state==1) {
                                    var url = "{{urlAdmin('favorable','getDelFavorable')}}?id=" + id;
                                }else{
                                    var url = "{{urlAdmin('favorable','getDelFavorableUsed')}}?id=" + id;
                                }
                                layer.confirm('您确定要删除吗？', {
                                    btn: ['确定','取消'] //按钮
                                }, function(){
                                    $.ajax({
                                        type:"get",
                                        url: url,
                                        dataType:"json",
                                        success:function(res){
                                             if(res['state']==1){
                                                 var parent = _this.parentNode.parentNode;
                                                 document.getElementById('table').removeChild(parent);
                                                 layer.msg('删除成功', {icon: 6});
                                             }else{
                                                 layer.msg('删除失败', {icon: 5});
                                             }
                                        }
                                    })

                                });
                            }

                            function writeOff(id){
                                layer.open({
                                    type: 1,
                                    title:"优惠券核销",
                                    skin: 'layui-layer-rim', //加上边框
                                    area: ['420px', '240px'], //宽高
                                    content: '<div id="write_content"><p>请输入你的兑换码</p><input type="hidden" value="'+id+'" id="favid"><input type="text" class="w_input" id="code"><div id="button"><a href="javascript:void(0)" onclick="confirmWrite_off()" class="btn">确认</a><a href="" class="btn">取消</a></div></div>'
                                });
                            }
                            function confirmWrite_off(){
                                  var fav_id = $('#favid').val();
                                  var code = $('#code').val();
                                if(code==''){
                                     layer.msg("兑换码不能为空");
                                     return ;
                                }
                                $.ajax({
                                    type:"post",
                                    url:"{{urlAdmin('favorable','postWriteOff')}}",
                                    data:{fav_id:fav_id,code:code},
                                    dataType:"json",
                                    success:function(data){
                                          if (data['state']==1){
                                              layer.msg('核销成功', {icon: 6});
                                          }else{
                                              layer.msg(data['msg'],{icon: 5});
                                          }
                                    }
                                })
                            }

                            function Write_off(){
                                var code = $('#code').val();
                                if(code==''){
                                    layer.msg("兑换码不能为空");
                                    return ;
                                }
                                $.ajax({
                                    type:"post",
                                    url:"{{urlAdmin('favorable','postWriteOff')}}",
                                    data:{code:code},
                                    dataType:"json",
                                    success:function(data){
                                        if (data['state']==1){
                                            layer.msg('核销成功', {icon: 6});
                                        }else{
                                            layer.msg(data['msg'],{icon: 5});
                                        }
                                    }
                                })
                            }
                        </script>
                    @endsection
                </form>
            </div>
        </div>
    </div>
    </div>
@endsection